package com.zj.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.zj.bean.Command;
import com.zj.bean.Custom;
import com.zj.bean.Mth;

import cn.hn.java.summer.db.BaseDao;
import cn.hn.java.summer.db.multiple.DataSource1;

@Service
public class ActService extends BaseDao<DataSource1>{
	public List<Custom> custom(Custom cs){
		System.out.println(cs);
		String cusName = cs==null?null:cs.getCusName();
    	System.out.println("custom： "+cusName);
    	return this.list("getCustom",Custom.class,cusName);
    }
	public int insertCustom(Custom cs){
		System.out.println("insertCustom");
		Integer id = this.get("getMaxCusId",Integer.class);
		if(id==null)id=1;
		cs.setId(id+1);
		return this.update("insertCustom",cs);
	}
	public void delCustom(int id){
		this.update("delCustom",id);
	}
	public int updateCustom(Custom cs){
		System.out.println("updateCustom");
		return this.update("updateCustom",cs);
	}
	
	 public List<Command> command(Command cmd){
		 System.out.println(cmd);
		String cusName = cmd==null?null:cmd.getCusName();
    	System.out.println("command： "+cusName);
    	List<Command> clist = this.list("getCommand",Command.class,cusName);
    	List<Mth> mlist = getCmdsLeft();
    	if(mlist == null || clist == null)return clist;
    	for(Command cm : clist){
    		for(Mth mth : mlist){
    			if(cm.getId() == mth.getCmdId()){
    				cm.setTotalNum(mth.getTotal());
    				cm.setLeftNum(mth.getTotal() - mth.getDel());
    				continue;
    			}
    		}
    	}
    	return clist;
	 }
	 public List<Mth> getCmdsLeft(){
		 System.out.println("command");
		 return this.list("getCmdsLeft",Mth.class);
	 }
	 
	 
	 public int insertcommand(Command cmd){
			System.out.println("insertCustom");
			Integer id = this.get("getMaxCmdId",Integer.class);
			if(id==null)id=1;
			cmd.setId(id+1);
			System.out.println("2: "+cmd);
			return this.update("insertCommand",cmd);
		}
		public void delCommand(int id){
			this.update("delCommand",id);
		}
		public int updateCommand(Command cmd){
			System.out.println("updateCommand");
			return this.update("updateCommand",cmd);
		}
		
		public int insertMth(Mth mth){
			Integer id = this.get("getMaxMthId",Integer.class);
			if(id==null)id=1;
			mth.setId(id);
			System.out.println("insertMth: "+mth);
			return this.update("insertMth",mth);
		}
}
